﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LPV_POS;
using System.Windows.Forms;
using System.IO.Ports;
using System.Drawing;

public class Variable
{
    //sys_user
    public static string username = "";//DM_DANGNHAP.USERNAME
    public static string ma_khohang = "";

    public static DateTime ngay_lamviec;//
    public static DateTime ngay_gio_hienhanh;//
    public static DataTable tbBH_CAU_HINH;//2010.11.12
    public static DataTable tbDM_LOAIDICHVU;//2011.07.21
    
    //public static DataTable tbMENU_QUYEN;//2010.11.12
    public static int iquyen;//2010.12.15

    // mh_select_hanghh
    //public static string str_manhomchu = "";
    //public static string str_manhomhang = "";
    //public static string str_mahh = "";
    //public static Int16 int_trangthai;
    //public static string str_makho;


    //mh_so
    public static int int_vitri;
    public static string str_position_frmso = "";
    public static string str_so = "";

    // mh_thongbao
    public static Boolean thongbao;

    // mh_select_hhlienket
    public static string str_mahhlienket = "";
    public static string str_tenhh = "";
    public static string str_soluong = "";
    public static Boolean bool_close = false;


    // mh_ thuchi
    public static string str_chu = "";
    public static string str_trangthaithemsua = "";
    public static string str_trangthaithemsuachi = "";

    // mh_dodvt
    public static string str_position_frmchu = "";
    public static int pont_x = 0;
    public static int pont_y = 0;

    //mh_hanghoa
    public static string str_tuychon = "";
    //public static DataTable tb;

    //------------------------------------------------ Begin cấu hình báo cáo -----------------------------------
    public static string str_tencongty = "";
    public static string str_diachicongty = "";
    public static string str_masothuecongty = "";
    public static string str_chuky1 = "";
    public static string str_chuky2 = "";
    public static string str_chuky3 = "";
    public static string str_chuky4 = "";
    public static string str_chuky5 = "";
    public static string str_tencongty_true = "";
    public static string str_diachicongty_true = "";
    public static string str_masothuecongty_true = "";
    public static string str_chuky1_true = "";
    public static string str_chuky2_true = "";
    public static string str_chuky3_true = "";
    public static string str_chuky4_true = "";
    public static string str_chuky5_true = "";
    public static string str_ngaythangnam = "";
    public static string str_tungay = "";
    public static string str_denngay = "";
    public static string str_tenhanghoa = "";
    public static string str_baocaotatca = "";
    //public static DataSet dset_baocao = null;
    public static string str_path = "";
    public static string str_intatca = "";
    public static string str_inhienhanh = "";
    public static string str_indoanhthu = "";
    public static Color maunen = System.Drawing.Color.Transparent;
    public static Color mauchu = System.Drawing.Color.Black;
    public static string str_chu_nen = "";
    public static int int_close = 1;
    public static Boolean baocaotrongngay = true;
    //public static string manhanvien = "";



    //------------------------------------------------ End cấu hình báo cáo -----------------------------------


    //public const string gconStrCopyRight = "Copyright 2010,Developed by LY PHU VINH";
    //public const string gconStrCopyRightK58 = "2010 C, LY PHU VINH";
    //public const string gconStrPhone = "DT:(08) 39.100.680";

    public static SerialPort_LPV[] gsrlCOMBillport; //mảng các cổng Com của máy tính(Bill)
    //public static SerialPort_LPV[] gsrlCOMKitChenport; //mảng các cổng Com của máy tính(Kitchen)

    public static DataRow[] arrDM_MAYIN_HD;
    public static DataRow[] arrDM_MAYIN_BEP;
    public static DataRow[] arrDM_MAYIN_BIENNHAN;
    //public static byte iPrinting = 0; //=1 đang in hóa đơn,0;chưa in
    //public static byte iPrintOff = 0; //=1 tắt hóa đơn,0: mở in hóa đơn
    public static string strComputer_Code = "00";//lưu mã máy tính cài phần mềm

    #region "   Get_DM_MAYIN_BIENNHAN"
    public byte Get_DM_MAYIN_BIENNHAN()
    {
        byte tempGet_DM_MAYIN_BIENNHAN = 0;
        thuvien othuvien = new thuvien();
        try
        {
            tempGet_DM_MAYIN_BIENNHAN = 0;
            string sSQL = "SELECT * FROM DM_MAYIN_BIENNHAN";// WHERE KIND = 0";
            DataTable tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            Variable.arrDM_MAYIN_BIENNHAN = tb.Select("TRIM(MAMAY) LIKE '" + Variable.strComputer_Code + "'");

            tempGet_DM_MAYIN_BIENNHAN = 1;

        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(2) + 
                            System.Environment.NewLine + "mdlVarialble - Get_DM_MAYIN_BIENNHAN" + System.Environment.NewLine + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
        return tempGet_DM_MAYIN_BIENNHAN;
    }
    #endregion

    #region "   Get_DM_MAYIN_HD"
    public  byte Get_DM_MAYIN_HD()
    {
        byte tempGet_DM_MAYIN_HD = 0;
        thuvien othuvien = new thuvien();
        try
        {
            tempGet_DM_MAYIN_HD = 0;
            string sSQL = "SELECT * FROM DM_MAYIN_HD";// WHERE KIND = 0";
            DataTable tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            Variable.arrDM_MAYIN_HD = tb.Select("TRIM(MAMAY) LIKE '" + Variable.strComputer_Code + "'");

            tempGet_DM_MAYIN_HD = 1;

        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(2) + 
                            System.Environment.NewLine + "mdlVarialble - Get_DM_MAYIN_HD" + System.Environment.NewLine + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
        return tempGet_DM_MAYIN_HD;
    }
    #endregion

    #region "   Get_DM_MAYIN_BEP"
    public byte Get_DM_MAYIN_BEP()
    {
        byte tempGet_DM_MAYIN_BEP = 0;
        thuvien othuvien = new thuvien();
        try
        {
            tempGet_DM_MAYIN_BEP = 0;
            string sSQL = "SELECT * FROM DM_MAYIN_BEP";
            DataTable tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            arrDM_MAYIN_BEP = tb.Select("TRIM(MAMAY) LIKE '" + Variable.strComputer_Code + "'");

            tempGet_DM_MAYIN_BEP = 1;

        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(2) + 
                            System.Environment.NewLine + "mdlVarialble - Get_DM_MAYIN_BEP" + System.Environment.NewLine + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
        return tempGet_DM_MAYIN_BEP;
    }
    #endregion

    #region "   Get_DM_MAYIN_BEP"
    public DataTable Get_DM_MAYIN_BEP1()
    {
        thuvien othuvien = new thuvien();
        DataTable tb = new DataTable();
        try
        {
            string sSQL = "SELECT * FROM DM_MAYIN_BEP WHERE COMNAME LIKE 'COM%'";
            tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL",
                                    new string[] { "@sSQL" },
                                    new object[] { sSQL });
        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(2) + 
                            System.Environment.NewLine + "mdlVarialble - Get_DM_MAYIN_BEP" + System.Environment.NewLine + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
        return tb;
    }
    #endregion


    #region "   SerialPort_Open_Receipt"
    /// <summary>
    /// Chức năng: Khởi tạo cổng COM in 
    /// </summary>
    /// <returns></returns>
    public Boolean SerialPort_Open_Receipt()
    {
        Boolean tempgfOpenSerialPort = false;
        int intLoop = 0;
        try
        {
            if (arrDM_MAYIN_BIENNHAN.Length == 0)
            {
                return tempgfOpenSerialPort;
            }

            gsrlCOMBillport = new SerialPort_LPV[arrDM_MAYIN_BIENNHAN.Length];
            string[] sCOM = SerialPort_LPV.GetPortNames();

            for (intLoop = 0; intLoop < gsrlCOMBillport.Length; intLoop++)
            {
                //if (arrDM_MAYIN_BIENNHAN[intLoop]["KIND"] + "" == "0" || arrDM_MAYIN_BIENNHAN[intLoop]["KIND"] + "" == "1" || arrDM_MAYIN_BIENNHAN[intLoop]["KIND"] + "" == "2")
                string sComName = arrDM_MAYIN_BIENNHAN[intLoop]["COMNAME"] + "";
                if (sCOM.Contains(sComName))
                {
                    gsrlCOMBillport[intLoop] = new SerialPort_LPV();
                    gsrlCOMBillport[intLoop].OBJNAME = sComName;
                    gsrlCOMBillport[intLoop].PortName = System.Convert.ToString(arrDM_MAYIN_BIENNHAN[intLoop]["COMNAME"] + "").Trim(' ');
                    gsrlCOMBillport[intLoop].BaudRate = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(arrDM_MAYIN_BIENNHAN[intLoop]["BAUDRATE"].ToString()));
                    gsrlCOMBillport[intLoop].DataBits = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(arrDM_MAYIN_BIENNHAN[intLoop]["DATABITS"].ToString()));
                    try
                    {
                        if (gsrlCOMBillport[intLoop].IsOpen == false)
                            gsrlCOMBillport[intLoop].Open();
                    }
                    catch (Exception ex)
                    {
                        string s = ex.Message;
                    }
                }
            }

            tempgfOpenSerialPort = true;
        }
        catch (Exception ex)
        {
            //iPrinting = 1;
            //MessageBox.Show(//gArrStrMess(34) + 
            //                System.Environment.NewLine + "mdlPrintReceipt - SerialPort_Open " + ex.Message + System.Environment.NewLine, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            //iPrinting = 0;
        }

        return tempgfOpenSerialPort;
    }
    #endregion

    #region "   SerialPort_Open BILL"
    /// <summary>
    /// Chức năng: Khởi tạo cổng COM in HD
    /// </summary>
    /// <returns></returns>
    public Boolean SerialPort_Open()
    {
        Boolean tempgfOpenSerialPort = false;
        int intLoop = 0;
        try
        {
            if (arrDM_MAYIN_HD.Length == 0)
            {
                return tempgfOpenSerialPort;
            }
            
            gsrlCOMBillport = new SerialPort_LPV[arrDM_MAYIN_HD.Length];
            string[] sCOM = SerialPort_LPV.GetPortNames();
            System.Collections.ArrayList arr = new System.Collections.ArrayList();

            for (intLoop = 0; intLoop < gsrlCOMBillport.Length; intLoop++)
            {
                //if (arrDM_MAYIN_HD[intLoop]["KIND"] + "" == "0" || arrDM_MAYIN_HD[intLoop]["KIND"] + "" == "1" || arrDM_MAYIN_HD[intLoop]["KIND"] + "" == "2")
                string sComName = arrDM_MAYIN_HD[intLoop]["COMNAME"] + "";
                if (sCOM.Contains(sComName) && !arr.Contains(sComName))
                {
                    arr.Add(sComName);

                    gsrlCOMBillport[intLoop] = new SerialPort_LPV();
                    gsrlCOMBillport[intLoop].OBJNAME = sComName;
                    gsrlCOMBillport[intLoop].PortName = System.Convert.ToString(arrDM_MAYIN_HD[intLoop]["COMNAME"] + "").Trim(' ');
                    gsrlCOMBillport[intLoop].BaudRate = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(arrDM_MAYIN_HD[intLoop]["BAUDRATE"].ToString()));
                    gsrlCOMBillport[intLoop].DataBits = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(arrDM_MAYIN_HD[intLoop]["DATABITS"].ToString()));
                    try
                    {
                        if (gsrlCOMBillport[intLoop].IsOpen == false)
                            gsrlCOMBillport[intLoop].Open();
                    }
                    catch (Exception ex)
                    {
                        string s = ex.Message;
                    }
                }
            }

            tempgfOpenSerialPort = true;
        }
        catch (Exception ex)
        {
            //iPrinting = 1;
            //MessageBox.Show(//gArrStrMess(34) + 
            //                System.Environment.NewLine + "mdlPrintReceipt - SerialPort_Open " + ex.Message + System.Environment.NewLine, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            //iPrinting = 0;
        }

        return tempgfOpenSerialPort;
    }
    #endregion

    #region "   SerialPort_Open_KitChen"
    /// <summary>
    /// Chức năng:Khởi tạo cổng COM in KitChen
    /// </summary>
    /// <returns></returns>
    public SerialPort_LPV[] SerialPort_Open_KitChen()
    {
        byte tempgfOpenSerialPort = 0;
        int intLoop = 0;
        //Boolean isOpen = false;
        SerialPort_LPV[] SerialPort_LPVs = new SerialPort_LPV[arrDM_MAYIN_BEP.Length];
        try
        {
            tempgfOpenSerialPort = 0;

            if (arrDM_MAYIN_BEP.Length == 0)
                return SerialPort_LPVs;// tempgfOpenSerialPort;

            //gsrlCOMBillport = new SerialPort_LPV[arrDM_MAYIN_BEP.Length];
            
            string[] sCOM = SerialPort_LPV.GetPortNames();
            for (intLoop = 0; intLoop < SerialPort_LPVs.Length; intLoop++)
            {
                string sComName = arrDM_MAYIN_BEP[intLoop]["COMNAME"] + "";
                string sObjName = arrDM_MAYIN_BEP[intLoop]["OBJNAME"] + "";
                string sObjId = arrDM_MAYIN_BEP[intLoop]["DM_MAYIN_BEP_MA"] + "";
                if (sCOM.Contains(sComName))
                {
                    SerialPort_LPVs[intLoop] = new SerialPort_LPV();
                    SerialPort_LPVs[intLoop].OBJNAME = sObjName;
                    SerialPort_LPVs[intLoop].OBJID = sObjId;
                    SerialPort_LPVs[intLoop].PortName = System.Convert.ToString(arrDM_MAYIN_BEP[intLoop]["COMNAME"] + "").Trim(' ');
                    SerialPort_LPVs[intLoop].BaudRate = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(arrDM_MAYIN_BEP[intLoop]["BAUDRATE"].ToString()));
                    SerialPort_LPVs[intLoop].DataBits = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(arrDM_MAYIN_BEP[intLoop]["DATABITS"].ToString()));
                    //isOpen = false;
                    //try
                    //{
                    //    //if (gsrlCOMBillport[intLoop].IsOpen == false)
                    //    //    gsrlCOMBillport[intLoop].Open();
                    //}
                    //catch (Exception ex)
                    //{
                    //    string s = ex.Message;
                    //}
                    //isOpen = true;
                }
                tempgfOpenSerialPort = 1;
            }
        }
        catch (Exception ex)
        {
            //iPrinting = 1;
            //if (!isOpen)
            //    gsrlCOMBillport = null;
            //else
            //    MessageBox.Show(//gArrStrMess(34) + 
            //                    System.Environment.NewLine + "mdlPrintReceipt - SerialPort_Open " + ex.Message + System.Environment.NewLine, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            //iPrinting = 0;
        }

        return SerialPort_LPVs;// tempgfOpenSerialPort;
    }
    public SerialPort_LPV SerialPort_Open_KitChen(DataRow drBep)
    {
        SerialPort_LPV SerialPort_LPVs = new SerialPort_LPV();
        try
        {
            string[] sCOM = SerialPort_LPV.GetPortNames();

            string sComName = drBep["COMNAME"] + "";
            string sObjName = drBep["OBJNAME"] + "";
            string sObjId = drBep["DM_MAYIN_BEP_MA"] + "";
            if (sCOM.Contains(sComName))
            {
                SerialPort_LPVs = new SerialPort_LPV();
                SerialPort_LPVs.OBJNAME = sObjName;
                SerialPort_LPVs.OBJID = sObjId;
                SerialPort_LPVs.PortName = System.Convert.ToString(drBep["COMNAME"] + "").Trim(' ');
                SerialPort_LPVs.BaudRate = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(drBep["BAUDRATE"].ToString()));
                SerialPort_LPVs.DataBits = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Val(drBep["DATABITS"].ToString()));
                return SerialPort_LPVs;

            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            //iPrinting = 0;
        }

        return SerialPort_LPVs;// tempgfOpenSerialPort;
    }
    #endregion


    #region "   SerialPort_Close_Receipt"
    /// <summary>
    /// Chức năng:Đóng cổng COM
    /// </summary>
    public void SerialPort_Close_Receipt()
    {
        try
        {
            //đóng cổng Com
            byte bytLoop = 0; //số máy in
            if (gsrlCOMBillport == null)
                return;

            for (bytLoop = 0; bytLoop < gsrlCOMBillport.Length; bytLoop++)
            {
                //if (arrDM_MAYIN_BIENNHAN[bytLoop]["KIND"] + "" == "0" || arrDM_MAYIN_BIENNHAN[bytLoop]["KIND"] + "" == "1" || arrDM_MAYIN_BIENNHAN[bytLoop]["KIND"] + "" == "2")
                {
                    if (gsrlCOMBillport[bytLoop] != null)
                    {
                        if (gsrlCOMBillport[bytLoop].IsOpen)
                        {
                            gsrlCOMBillport[bytLoop].Close();
                            //gsrlCOMBillport[bytLoop] = null;
                        }
                        gsrlCOMBillport[bytLoop].Dispose();
                    }
                }
            }
            gsrlCOMBillport = null;

        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(34) + 
                            System.Environment.NewLine + "mdlPrintReceipt - SerialPort_Open " + ex.Message + System.Environment.NewLine, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
    }
    #endregion

    #region "   SerialPort_Close"
    /// <summary>
    /// Chức năng:Đóng cổng COM
    /// </summary>
    public void SerialPort_Close()
    {
        try
        {
            //đóng cổng Com
            byte bytLoop = 0; //số máy in
            if (gsrlCOMBillport == null)
                return;

            for (bytLoop = 0; bytLoop < gsrlCOMBillport.Length; bytLoop++)
            {
                //if (arrDM_MAYIN_HD[bytLoop]["KIND"] + "" == "0" || arrDM_MAYIN_HD[bytLoop]["KIND"] + "" == "1" || arrDM_MAYIN_HD[bytLoop]["KIND"] + "" == "2")
                {
                    if (gsrlCOMBillport[bytLoop] != null)
                    {
                        if (gsrlCOMBillport[bytLoop].IsOpen)
                        {
                            gsrlCOMBillport[bytLoop].Close();
                            //gsrlCOMBillport[bytLoop] = null;
                        }
                        gsrlCOMBillport[bytLoop].Dispose();
                    }
                }
            }
            gsrlCOMBillport = null;

        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(34) + 
                            System.Environment.NewLine + "mdlPrintReceipt - SerialPort_Open " + ex.Message + System.Environment.NewLine, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
    }
    #endregion

    #region "   SerialPort_Close_KitChen"
    /// <summary>
    /// Chức năng:Đóng cổng COM
    /// </summary>
    public void SerialPort_Close_KitChen()
    {
        try
        {
            //đóng cổng Com
            byte bytLoop = 0; //số máy in
            if (gsrlCOMBillport == null)
                return;

            for (bytLoop = 0; bytLoop < gsrlCOMBillport.Length; bytLoop++)
            {
                if (gsrlCOMBillport[bytLoop] != null)
                {
                    if (gsrlCOMBillport[bytLoop].IsOpen)
                    {
                        gsrlCOMBillport[bytLoop].Close();
                        //gsrlCOMBillport[bytLoop] = null;
                    }
                    gsrlCOMBillport[bytLoop].Dispose();
                }
            }
            gsrlCOMBillport = null;

        }
        catch (Exception ex)
        {
            LPV_POS.Module.MessageBox_LPV.Show(//gArrStrMess(34) + 
                            System.Environment.NewLine + "mdlPrintReceipt - SerialPort_Open " + ex.Message + System.Environment.NewLine, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
        }
    }
    #endregion


    

}


